package com.jmu.edu.IMApplication.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jmu.edu.IMApplication.R;
import com.jmu.edu.IMApplication.event.LoadCustomText;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class CustomTextItemAdapter extends RecyclerView.Adapter<CustomTextItemAdapter.MyViewHolder> {
    private List<String> contentList;

    public void setContentList(List<String> contentList) {
        this.contentList = contentList;
    }

    public void addContentList(List<String> contentList) {
        this.contentList.addAll(contentList);
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_custom_text, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        String content = contentList.get(position);
        holder.content.setText(content);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //发送更新信息的事件
                EventBus.getDefault().post(new LoadCustomText(content));
            }
        });
    }

    @Override
    public int getItemCount() {
        return contentList.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView content;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            content = itemView.findViewById(R.id.item_custom_text_content);

        }
    }
}
